package com.kevin.enterprise.dao;

import com.kevin.enterprise.entity.Student;
import com.kevin.enterprise.utils.JdbcUtils;

import javax.sql.rowset.JdbcRowSet;
import javax.swing.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class StudentDao {
    Connection conn = null;
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    String sql = null;

    public List<Student> studentList() {

        try {
            //int userName1 = Integer.parseInt(userName);
            //sql = "select * from student where id=? and name=?";
            sql = "select * from student";
            conn = JdbcUtils.getConnection();
            pstmt = conn.prepareStatement(sql);
            rs = pstmt.executeQuery();
            List<Student> list = new ArrayList<>();
            while (rs.next()) {
                Student student = new Student();
                student.setSno(rs.getString("sno"));
                student.setPassword(rs.getString("password"));
                student.setSname(rs.getString("sname"));
                student.setSsex(rs.getString("ssex"));
                student.setSpeciality(rs.getString("speciality"));
                student.setSbirthday(rs.getDate("sbirthday"));
                student.setTc(rs.getInt("tc"));
                list.add(student);
                //System.out.println(student.getSno());
            }
            //System.out.println(list);
            return list;

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JdbcUtils.getClose(conn, pstmt, rs);
        }
        return null;
    }

    public boolean delectByID(String id) {
        String sql = "delete from student where sno=?";
        try {
            conn = JdbcUtils.getConnection();
            conn.setAutoCommit(false);
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, id);
            pstmt.executeUpdate();
            conn.commit();
            return true;

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JdbcUtils.getClose(conn, pstmt, rs);
        }
        return false;
    }

    public boolean addByOne(Student student) {
        try {
            conn = JdbcUtils.getConnection();
            sql = "insert into student(sno,password,sname,ssex) values (?,?,?,?)";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, student.getSno());
            pstmt.setString(2, student.getPassword());
            pstmt.setString(3, student.getSname());
            pstmt.setString(4, student.getSsex());
            //pstmt.setDate(3,student.getSbirthday());
            pstmt.executeUpdate();
            conn.commit();
            return true;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JdbcUtils.getClose(conn, pstmt, rs);
        }
        return false;
    }

    public Student updateByOne(String sno) {
        try {
            conn = JdbcUtils.getConnection();
            sql = "SELECT * FROM student WHERE sno=?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, sno);
            rs = pstmt.executeQuery();
            Student student = new Student();
            if (rs.next()) {
                student.setSno(rs.getString("sno"));
                student.setPassword(rs.getString("password"));
                student.setSname(rs.getString("sname"));
                student.setSsex(rs.getString("ssex"));
                student.setSpeciality(rs.getString("speciality"));
                student.setSbirthday(rs.getDate("sbirthday"));
                student.setTc(rs.getInt("tc"));
            }
            return student;

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JdbcUtils.getClose(conn, pstmt, rs);
        }
        return null;
    }

    public boolean modifystudent(Student student) {


        sql = "UPDATE student SET sname=?,password=?,ssex=?,speciality=?,tc=?,sbirthday=? WHERE  sno=?";

        try {
            conn = JdbcUtils.getConnection();
            conn.setAutoCommit(false);
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, student.getPassword());
            pstmt.setString(2, student.getSname());
            pstmt.setString(3, student.getSsex());
            pstmt.setString(4, student.getSpeciality());
            pstmt.setInt(5, student.getTc());
            pstmt.setDate(6, student.getSbirthday());
            pstmt.setString(7, student.getSno());
            pstmt.executeUpdate();
            conn.commit();
            return true;
            //request.getRequestDispatcher("student").forward(request,response);

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JdbcUtils.getClose(conn, pstmt, rs);
        }
        return false;
    }

}
